<template>
  <el-card
    class="box-card"
    style="background-color: #fff; margin-left: 1%; width: 98%"
  >
    <div
      style="
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #ebeef5;
      "
    >
      <p style="line-height: 33px">商品分类</p>
      <el-button @click="clickAdd" style="margin-bottom: 5px" type="primary"
        >添加分类</el-button
      >
    </div>

    <!-- 表格 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" width="100"> </el-table-column>
      <el-table-column label="分类名称" width="180">
        <template slot-scope="obj">
          <el-input
            type="text"
            v-model="obj.row.cateName"
            v-if="obj.row.showEdit"
          ></el-input>
          <span v-else>{{ obj.row.cateName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="是否启用" width="180">
        <template slot-scope="scope">
          <el-switch
            :disabled="!scope.row.showEdit"
            v-model="scope.row.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="obj">
          <el-button
            size="mini"
            v-if="obj.row.showEdit"
            @click="ensure(obj.row)"
            >完成</el-button
          >
          <el-button size="mini" v-else @click="celledit(obj.row)"
            >编辑</el-button
          >

          <el-button size="mini" type="danger" @click="clickDel(obj.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      style="height: 50px; padding-top: 15px; margin-bottom: 10px"
      @size-change="sizeChange"
      @current-change="curChange"
      :current-page="currentPage"
      :page-sizes="[4, 6, 8, 10]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 添加分类蒙层 -->
    <div v-show="isshow" class="mask">
      <div class="mask-card">
        <div style="display: flex; justify-content: space-between">
          <p>添加分类</p>
          <i @click="clickCancel" class="el-icon-close"></i>
        </div>
        <div style="display: flex; margin-top: 30px; margin-left: 10px">
          <p style="font-size: 14px; margin-top: 12px">分类名称</p>
          <el-input
            style="width: 398px; margin-left: 10px"
            placeholder="分类名称"
            v-model="classify"
            clearable
          >
          </el-input>
        </div>
        <div style="margin-top: 30px; margin-left: 10px">
          <span style="font-size: 14px">是否启用</span>
          <el-switch
            style="margin-left: 10px"
            v-model="isEnable"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
        </div>
        <div class="button-box">
          <el-button size="medium">取消</el-button>
          <el-button @click="clickConfirm" size="medium" type="primary"
            >确定</el-button
          >
        </div>
      </div>
    </div>
  </el-card>
</template>

<script>
import { addcate, catelist, delcate, editcate } from "@/apis/goods";
export default {
  data() {
    return {
      isshow: false,
      classify: "", //分类名称
      isEnable: "", //是否禁用
      list: [],
      state: true, //按钮状态
      currentPage: 1, //当前页
      pagesize: 4, //每页显示条数
      total: 0, //总条数
    };
  },
  created() {
    this.reqtableData(); //初始化调用
  },
  methods: {
    //添加按钮
    clickAdd() {
      this.isshow = true;
    },

    //确定----蒙层
    clickConfirm() {
      addcate({
        cateName: this.classify,
        state: this.isEnable,
      }).then((res) => {
        console.log(res);
        if (res.data.code === 0) {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
          this.isshow = false;
          this.reqtableData();
        }
      });
    },

    reqtableData() {
      catelist({
        currentPage: this.currentPage,
        pageSize: this.pagesize,
      }).then((res) => {
        for (let obj of res.data.data) {
          obj.state = obj.state == 1;
          obj.showEdit = false; //当前数据是否点击了编辑
        }
        // console.log(res);
        this.list = res.data.data;
        this.total = res.data.total;
      });
    },

    clickCancel() {
      this.isshow = false;
    },

    //当前页输入框
    sizeChange(val) {
      this.currentPage = val;
      this.reqtableData();
    },

    //点击当前页
    curChange(val) {
      this.currentPage = val;
      this.reqtableData();
    },

    //删除
    clickDel(row) {
      // console.log(row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delcate({
            id: row.id,
          }).then((res) => {
            if (res.data.code === 0) {
              this.reqtableData();
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // handleEdit(index, row) {
    //   console.log(index, row);
    // },

    //编辑
    celledit(row) {
      row.showEdit = true; //点击编辑
    },

    //完成
    ensure(row) {
      // console.log(index, row);
      row.showEdit = false;
      editcate({
        id: row.id,
        cateName: row.cateName,
        state: row.state,
      }).then((res) => {
        // console.log(res);
        if (res.data.code === 0) {
          this.$message({
            message: res.data.msg,
            type: "success",
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.mask {
  z-index: 10;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(120, 121, 123, 0.8);
  .mask-card {
    position: absolute;
    top: 30%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 500px;
    height: 300px;
    padding: 10px;
    background-color: #fff;
    .button-box {
      position: absolute;
      bottom: 30px;
      right: 20px;
    }
  }
}
</style>